package com.pactera.asmp.server.restfullapi;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.pactera.asmp.server.common.constants.Constants;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.logprint.utils.StringUtils;
import com.pactera.asmp.server.entity.LaboratoryInfoDTO;
import com.pactera.asmp.server.entity.LaboratoryInfoSearchDTO;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.service.ICameraService;
import com.pactera.asmp.server.service.ILaboratoryInfoService;
import com.pactera.asmp.server.utils.PageDataResult;

@RestController
@RequestMapping("/api/v1")
public class RestfulLaboratoryApi {

    private static final Logger logger = LoggerFactory.getLogger(RestfulLaboratoryApi.class);

    @Autowired
    private ILaboratoryInfoService laboratoryInfoService;

    @Autowired
    private ICameraService cameraService;

    /**
     * 查询实验室
     * 
     * @param laboratoryInfoSearchDTO
     * @return
     */
    @PostMapping(value = "/laboratory/list", produces = "application/json;charset=UTF-8")
    public ResponseResult getLaboratoryList(@RequestBody LaboratoryInfoSearchDTO laboratoryInfoSearchDTO) {
        logger.info("POST /api/v1/laboratory/list getLaboratoryList");
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        PageDataResult pdr;
        JSONObject jsonObject = new JSONObject();
        if (null == laboratoryInfoSearchDTO.getPage() || laboratoryInfoSearchDTO.getPage().intValue() == 0) {
            laboratoryInfoSearchDTO.setPage(1);
        }
        if (null == laboratoryInfoSearchDTO.getLimit() || laboratoryInfoSearchDTO.getLimit().intValue() == 0) {
            laboratoryInfoSearchDTO.setLimit(10);
        }

        if (laboratoryInfoSearchDTO.getPage().intValue() < 0 || laboratoryInfoSearchDTO.getLimit().intValue() < 0) {
            jsonObject.put("totals", 0);
            jsonObject.put("list", Lists.newArrayList());
            responseResult.setData(jsonObject);
            return responseResult;
        }
        pdr = laboratoryInfoService.select(laboratoryInfoSearchDTO);
        jsonObject.put("totals", pdr.getTotals());
        jsonObject.put("list", pdr.getList());
        responseResult.setData(jsonObject);
        return responseResult;
    }

    /**
     * 根据实验室id查询摄像头
     * 
     * @param labId
     * @return
     */
    @GetMapping(value = "/laboratory/cameraListByLabId", produces = "application/json;charset=UTF-8")
    public ResponseResult cameraListByLabId(@RequestParam(value = "labId", required = false) Integer labId) {
        logger.info("get /api/v1/laboratory/cameraListByLabId cameraListByLabId");
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(cameraService.select(labId));
        return responseResult;
    }

    /**
     * 新建实验室
     * 
     * @param laboratoryInfoDTO
     * @return
     */
    @PostMapping(value = "/laboratory",  produces = "application/json;charset=UTF-8")
    public ResponseResult addLaboratory(@RequestBody LaboratoryInfoDTO laboratoryInfoDTO) {
        logger.info("POST /api/v1/laboratory addLaboratory");
        if (null == laboratoryInfoDTO) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtils.isEmpty(laboratoryInfoDTO.getLabName())
                || laboratoryInfoDTO.getProjectId() == null
                || laboratoryInfoDTO.getStatus() == null) {
            return new ResponseResult(ResultCode.PARAM_NOT_COMPLETE);
        }
        if (!laboratoryInfoDTO.getLabName().matches(Constants.REGEXP_FIRST_LAST_NOT_SPACE)) {
            return new ResponseResult(ResultCode.PARAM_TYPE_BIND_ERROR);
        }
        return ResponseResult.defaultSuccess(laboratoryInfoService.insert(laboratoryInfoDTO));
    }

    /**
     * 修改实验室
     * 
     * @param laboratoryInfoDTO
     * @return
     */
    @PutMapping(value = "/laboratory",  produces = "application/json;charset=UTF-8")
    public ResponseResult updateLaboratory(@RequestBody LaboratoryInfoDTO laboratoryInfoDTO) {
        logger.info("PUT /api/v1/laboratory updateLaboratory");
        if (laboratoryInfoDTO == null
                || laboratoryInfoDTO.getId() == null
                        || laboratoryInfoDTO.getProjectId() == null
                        || laboratoryInfoDTO.getStatus() == null) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        if (!laboratoryInfoDTO.getLabName().matches(Constants.REGEXP_FIRST_LAST_NOT_SPACE)) {
            return new ResponseResult(ResultCode.PARAM_TYPE_BIND_ERROR);
        }
        return laboratoryInfoService.update(laboratoryInfoDTO) ? new ResponseResult(ResultCode.SUCCESS) : new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
    }

    /**
     * 删除实验室
     * 
     * @param labId
     * @return
     */
    @DeleteMapping(value = "/laboratory", produces = "application/json;charset=UTF-8")
    public ResponseResult deleteLaboratory(Integer labId) {
        logger.info("delete /api/v1/laboratory deleteLaboratory ");
        if (null == labId) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return laboratoryInfoService.delete(labId) ? new ResponseResult(ResultCode.SUCCESS) : new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
    }

}
